﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EasyNet.Solr;
using EasyNet.Solr.Commons;
using EasyNet.Solr.Impl;
using EasyNet.Solr.Commons.Params;

namespace SolrExample.Tests.EasyNetSolr
{
    public class EasyNetEngineCommand:ISolrEngineCommand
    {
        private const char engineUrlSeparator = '#';
        private CommitOptions commitOptions = new CommitOptions();
        private OptimizeOptions optimizeOptions = new OptimizeOptions();
        private static readonly ISolrResponseParser<NamedList, ResponseHeader> binaryResponseHeaderParser = new BinaryResponseHeaderParser();

        private static readonly IObjectSerializer<SolrIndexRecord> objectSerializer = new SolrObjectSerializer();
        private static readonly IUpdateParametersConvert<NamedList> updateParametersConvert = new BinaryUpdateParametersConvert();
        private ISolrUpdateConnection<NamedList, NamedList> solrUpdateConnection;
        private ISolrUpdateOperations<NamedList> updateOperations;
        
        private ISolrReadonlyConnection<NamedList> solrQueryConnection;
        private ISolrQueryOperations<NamedList> queryOperations;

        public IDictionary<string, bool> Post(List<string> engineUrls, string collectionName, IEnumerable<SolrInputDocument> solrInputDocument)
        {
            Dictionary<string, bool> results = new Dictionary<string, bool>();
            foreach (string engineUrl in engineUrls)
            {
                string engineCode = collectionName;
                string solrCoreUrl = engineUrl.Trim() + "/" + collectionName;
                if (engineUrl.Contains(engineUrlSeparator))
                {
                    var engineData = engineUrl.Split(engineUrlSeparator);
                    engineCode = engineData[0].Trim() + "-" + collectionName;
                    solrCoreUrl = engineData[1].Trim() + "/" + collectionName;
                }

                solrUpdateConnection = new SolrUpdateConnection<NamedList, NamedList>() { ServerUrl = solrCoreUrl };
                updateOperations = new SolrUpdateOperations<NamedList, NamedList>(solrUpdateConnection, updateParametersConvert)
                {
                    ResponseWriter = "javabin"
                };

                try
                {
                    var result = updateOperations.Update(new UpdateOptions()
                    {
                        OptimizeOptions = optimizeOptions,
                        Docs = solrInputDocument
                    });
                    //var header = binaryResponseHeaderParser.Parse(result);
                    //Console.WriteLine("Update Status:{0} QTime:{1}", header.Status, header.QTime);

                    results.Add(engineCode, true);
                }
                catch (Exception ex)
                {
                    results.Add(engineCode, false);
                }
            }
            return results;
        }

        public NamedList Query(string solrCoreUrl, SolrQuery solrQuery)
        {
            try
            {
                solrQueryConnection = new SolrReadonlyConnection<NamedList>() { ServerUrl = solrCoreUrl };
                queryOperations = new SolrQueryOperations<NamedList>(solrQueryConnection) { ResponseWriter = "javabin" };

                IDictionary<string, ICollection<string>> solrQueryOption = new Dictionary<string, ICollection<string>>();
                solrQueryOption[CommonParams.WT] = new string[] { "javabin" };
                //solrQueryOption[CommonParams.FL] = new string[] { "id", "name" };
                solrQueryOption[CommonParams.START] = new string[] { "0" };
                solrQueryOption[CommonParams.ROWS] = new string[] { "100" };
                solrQueryOption[CommonParams.SORT] = new string[] { "id desc,name desc" };

                solrQueryOption[HighlightParams.HIGHLIGHT] = new string[] { "true" };
                solrQueryOption[HighlightParams.FIELDS] = new string[] { "id" };

                return queryOperations.Query(solrQuery, solrQueryOption);
            }
            catch (Exception ex) {
                return null;
            }
        }
    }
}
